package com.zidian.teacher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.teacher.R;
import com.zidian.teacher.bean.PutStudyBean;
import com.zidian.teacher.common.BaseEntity;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.common.ToolBarActivity;
import com.zidian.teacher.utils.DataCheckUtil;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ProgressBarBlackUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/9/5 0005.
 */
public class TeachingWorkAddActivity extends ToolBarActivity implements View.OnClickListener {
    @Bind(R.id.work_title_edit)
    EditText workTitleEdit;
    @Bind(R.id.work_count_edit)
    EditText workCountEdit;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.teaching_work_set_time)
    LinearLayout teachingWorkSetTime;
    @Bind(R.id.study_class)
    TextView studyClass;
    @Bind(R.id.teaching_work_set_class)
    LinearLayout teachingWorkSetClass;
    @Bind(R.id.study)
    TextView study;
    @Bind(R.id.teaching_work_set_study)
    LinearLayout teachingWorkSetStudy;
    private TextView confrimWork;
    private DataCheckUtil data;
    private String courseId = "";
    private int mYear;
    private int mMonth;
    private int mDay;
    private PutStudyBean confrimBean = new PutStudyBean();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_both);
        setContentView(R.layout.activity_teaching_work_add);
        ButterKnife.bind(this);
        setTitle("新增作业");
        confrimWork = (TextView) findViewById(R.id.toolbar_sure);
        Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DATE);
        data = new DataCheckUtil(this, time);
        teachingWorkSetTime.setOnClickListener(this);
        teachingWorkSetClass.setOnClickListener(this);
        teachingWorkSetStudy.setOnClickListener(this);
        confrimWork.setOnClickListener(this);
        time.setText(mYear + "-" + (mMonth + 1) + "-" + mDay + " " + "12:00");


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000) {
            if (resultCode == 1001) {
                String result_value = data.getStringExtra("className");
                courseId = data.getStringExtra("courseId");
                studyClass.setText(result_value);

            } else if (resultCode == 1002) {
                confrimBean = (PutStudyBean) data.getSerializableExtra("study");
                if (confrimBean.getStudyBeans().size() != 0)
                    study.setText("(已选" + confrimBean.getStudyBeans().size() + "人)");
            }
        }
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.teaching_work_set_time:

                data.clickBirthday(mYear, mMonth, mDay);
                break;
            case R.id.teaching_work_set_class:
                intent.setClass(this, CourseSelectActivity.class);
                intent.putExtra("courseId", courseId);
                startActivityForResult(intent, 1000);
                break;
            case R.id.teaching_work_set_study:

                if (courseId.length() == 0) {
                    Snackbar.make(time,"请选择课程",Snackbar.LENGTH_SHORT).show();
                    return;
                } else {
                    intent.setClass(this, TeachingWorkStudySelectActivity.class);
                    intent.putExtra("courseId", courseId);
                    startActivityForResult(intent, 1000);
                }

                break;
            case R.id.toolbar_sure:
                addWork();
                break;
        }

    }

    private void addWork() {
        String student = "";

        try {
            JSONArray jsonarray = new JSONArray();

            if (confrimBean.getStudyBeans() != null) {
                for (int i = 0; i < confrimBean.getStudyBeans().size(); i++) {
                    JSONObject jsonObjAnswer = new JSONObject();//pet对象，json形式
                    jsonObjAnswer.put("studentId", confrimBean.getStudyBeans().get(i).getStudyID());
                    jsonarray.put(jsonObjAnswer);
                }
                student = jsonarray.toString();//生成返回字符串
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (workTitleEdit.getText().toString().trim().length() == 0) {
            Snackbar.make(time,"请输入作业标题",Snackbar.LENGTH_SHORT).show();
            return;
        } else if (courseId.length() == 0) {
            Snackbar.make(time,"请选择课程",Snackbar.LENGTH_SHORT).show();
            return;
        } else if (workCountEdit.getText().toString().trim().length() == 0) {
            Snackbar.make(time,"请输入作业内容",Snackbar.LENGTH_SHORT).show();
            return;
        } else if (workTitleEdit.getText().toString().trim().length() > 20) {
            Snackbar.make(time,"作业标题多于20个字符", Snackbar.LENGTH_SHORT).show();
            return;
        }else if (student.length() == 0) {
            Snackbar.make(time,"请选择学生",Snackbar.LENGTH_SHORT).show();
            return;
        }

        String loginUrl = Constant.URL_GOAL + "work/add";

        HashMap map = new HashMap();
        map.put("teacherId", GetUserInfo.getTeacherId(context));
        map.put("courseId", courseId);
        map.put("schoolWorkContent", workCountEdit.getText().toString().trim());
        map.put("workName", workTitleEdit.getText().toString().trim());
        map.put("handOverTime", time.getText().toString().trim());
        map.put("student", student);
        map.put("token", GetUserInfo.getToken(context));
        ProgressBarBlackUtil.loadShow(context);
        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("E", e.toString());
                ProgressBarBlackUtil.loadCancel();
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("T", response);
                Gson gson = new Gson();
                BaseEntity base = gson.fromJson(response, BaseEntity.class);
                if (base.isSuccess()) {
                    ProgressBarBlackUtil.loadCancel();
                    Snackbar.make(time,"提交成功",Snackbar.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.putExtra("hasChange", true);
                    setResult(4001, intent);
                    finish();
                }
            }
        });

    }


}
